<template>
  <div id="order" class="container">
    <!-- <div class="search">
      <el-form ref="searchForm" :model="searchForm" inline label-width="90px">
        <el-form-item label="内链名称：" prop="title">
          <el-input v-model="searchForm.title" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search(searchForm)">查询</el-button>
          <el-button @click="reset(searchForm)">重置</el-button>
        </el-form-item>
      </el-form>
    </div> -->
    <h3 class="title"><span />站点管理</h3>
    <el-button type="primary" class="add" size="small" icon="el-icon-plus" style="margin-bottom:15px" @click="goAdd">添加</el-button>
    <BouncedDialog :title="addForm.id?'编辑':'添加'" :dialog-show.sync="dialogShow" :width="50" class="link">
      <el-form ref="addForm" :model="addForm" :rules="rules" inline class="addForm" label-width="80px">
        <el-form-item label="网站名称" prop="title">
          <el-input v-model="addForm.title" />
        </el-form-item>
        <el-form-item label="网站域名" prop="domain">
          <el-input v-model="addForm.domain" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="addForm.status">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="2" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="模型类型" prop="type">
          <el-select v-model="addForm.type">
            <el-option label="独立模型" :value="1" />
            <el-option label="主表附表模型" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="addForm.description" />
        </el-form-item>
        <el-form-item label="系统模型">
          <el-select v-model="addForm.is_system">
            <el-option label="是" :value="1" />
            <el-option label="否" :value="2" />
          </el-select>
        </el-form-item> -->

        <div class="btn">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-form>
    </BouncedDialog>
    <el-table
      :data="orderData"
      border
      stripe
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
    >
      <el-table-column label="ID" prop="id" width="60px" />
      <el-table-column label="网站名称" prop="title" show-overflow-tooltip />
      <el-table-column label="域名" prop="domain" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="'http://'+scope.row.domain" target="_blank">{{ scope.row.domain }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="网站状态" prop="status" width="160px">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="2"
            active-text="正常"
            inactive-text="禁用"
            active-color="#f84311"
            inactive-color="rgba(0,0,0,0.2)"
            @change="disableUser(scope.row)"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="系统模型">
        <template slot-scope="scope">
          <span v-if="scope.row.is_system==1">是</span>
          <span v-if="scope.row.is_system==2">否</span>
        </template>
      </el-table-column>
      <el-table-column label="附表" prop="database_table" />
      <el-table-column label="描述" prop="description" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">正常</span>
          <span v-else-if="scope.row.status==2">禁用</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" fixed="right" width="200px">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="goIframe(scope.row)">自定义</el-button> -->
          <el-button class="edit" size="mini" @click="editor(scope.row)">编辑</el-button>
          <el-button class="dele" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total<page.size?true:false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import websiteApi from '@/api/website.js'

export default {
  data() {
    return {
      iframeState: false,
      orderData: [], //
      dialogShow: false,
      addForm: {
        id: '',
        title: '',
        domain: '',
        status: ''
      },
      rules: {
        title: [
          { required: 'true', message: '请输入网站名称', trigger: 'blur' }
        ],
        domain: [
          { required: 'true', message: '请输入网站域名', trigger: 'blur' }
        ]
      },
      searchForm: {
        title: ''
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {

      },
      id: '',
      reportUrl: ''
    }
  },
  created() {
    this.getList()
    this.reportUrl = 'http://localhost:9528/#/Link/index'
  },
  mounted() {
    // this.getTikets()
  },
  updated() {

  },
  methods: {
    disableUser(row) {
      const val = {
        id: row.id,
        title: row.title,
        domain: row.domain,
        status: row.status
      }

      websiteApi.websiteUpdate(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 跳转iframe
    goIframe(row) {
      // this.iframeState = true
      const routeData = this.$router.resolve({ name: 'iframe', query: { id: row.id }})
      window.open(routeData.href, '_blank')
    },
    // 新增
    goAdd() {
      this.id = ''
      this.addForm = {}
      this.addForm.status = 1
      this.dialogShow = true
    },
    // 编辑
    editor(row) {
      // this.id = row.id

      this.addForm = {
        title: row.title,
        domain: row.domain,
        status: row.status,
        id: row.id
      }
      this.dialogShow = true
    },
    submit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (!this.addForm.id) {
            websiteApi.websiteSave({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getWebList()
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            websiteApi.websiteUpdate({ ...this.addForm }).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.dialogShow = false
                this.getWebList()
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    },
    getWebList() {
      websiteApi.websiteList().then(res => {
        // 新增后的站点列表更新到vuex中
        this.$store.commit('tagsView/WEBLIST', res.data)
      })
    },
    // 查看模型字段列表
    check(row) {
      this.$router.push({ name: 'modelField', query: { id: row.id }})
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该模型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        websiteApi.websiteDelete({ id: row.id }).then(res => {
          if (res.code == 0) {
            this.$message.success('已删除')
            this.getList()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索
    search(searchForm) {
      this.$refs.searchForm.validate((valid) => {
        this.page.currentPage = 1
        this.getList(this.val)
      })
    },
    // 重置搜索框
    reset(searchForm) {
      this.searchForm.title = ''
    },
    // 模型列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      // this.val.title = this.searchForm.title
      websiteApi.websiteList(this.val).then(res => {
        this.orderData = res.data
        this.page.total = res.count
      })
    },

    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }

  }
}
</script>
<style lang='scss'>
#order{
  .link{
    .el-select{
        width: 100%;
    }
  }
  .addForm .el-form-item__content{
      width: calc(100% - 80px)
    }
}
</style>
<style scoped lang='scss'>
    .el-table{
      margin-bottom: 20px;
    }
    .search{
      min-width: 1350px;
      background-color: #fff;
      padding: 35px 30px 20px;
      border-radius: 5px;
    }
    .el-form-item{
      width: 100%;
    }

</style>
